package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.ProjectConstants;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.system.service.ISysHistoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysProjectInfo;
import com.ruoyi.system.service.ISysProjectInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目申报Controller
 *
 * @author riddou
 * @date 2022-04-11
 */
@RestController
@RequestMapping("/system/projectInfo")
public class SysProjectInfoController extends BaseController {
    @Autowired
    private ISysProjectInfoService sysProjectInfoService;

    @Autowired
    private ISysHistoryService sysHistoryService;

    /**
     * 查询项目申报列表
     */
    @PreAuthorize("@ss.hasPermi('system:projectInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysProjectInfo sysProjectInfo) {
        sysProjectInfo.setCreateBy(getUsername());
        startPage();
        List<SysProjectInfo> list = sysProjectInfoService.selectSysProjectInfoList(sysProjectInfo);
        return getDataTable(list);
    }

    /**
     * 导出项目申报列表
     */
    @PreAuthorize("@ss.hasPermi('system:projectInfo:export')")
    @Log(title = "项目申报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProjectInfo sysProjectInfo) {
        List<SysProjectInfo> list = sysProjectInfoService.selectSysProjectInfoList(sysProjectInfo);
        ExcelUtil<SysProjectInfo> util = new ExcelUtil<SysProjectInfo>(SysProjectInfo.class);
        util.exportExcel(response, list, "项目申报数据");
    }

    /**
     * 获取项目申报详细信息
     */
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId) {
        return AjaxResult.success(sysProjectInfoService.selectSysProjectInfoByProjectId(projectId));
    }

    /**
     * 添加项目
     */
    @PreAuthorize("@ss.hasPermi('system:projectInfo:add')")
    @Log(title = "项目申报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysProjectInfo sysProjectInfo) {
        sysProjectInfo.setCreateBy(getUsername());
        sysProjectInfo.setSubmitTime(new Date());
        return toAjax(sysProjectInfoService.insertSysProjectInfo(sysProjectInfo));
    }

    /**
     * 项目申报/申请验收
     */
    @PreAuthorize("@ss.hasPermi('system:projectInfo:edit')")
    @Log(title = "项目申报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysProjectInfo sysProjectInfo) {
        sysProjectInfo.setUpdateBy(getUsername());
        //申报项目
        if (ProjectConstants.STATE_COMMITED.equals(sysProjectInfo.getState())) {
            sysProjectInfo.setState(ProjectConstants.STATE_COMMITED);
            //异步处理提交信息
            AsyncManager.me().execute(AsyncFactory.processApplicate(sysProjectInfo));
        }
        //申请验收
        else {
            sysProjectInfo.setStage(ProjectConstants.STAGE_TO_ACCEPT);
            //d异步处理信息
            AsyncManager.me().execute(AsyncFactory.processAccpet(sysProjectInfo));
        }
        //申报/验收申请
        if (sysHistoryService.insertSysHistory(sysHistoryService.buildHistory(sysProjectInfo)) <= 0) return error();
        return toAjax(sysProjectInfoService.updateSysProjectInfo(sysProjectInfo));
    }

    /**
     * 删除项目申报
     */
    @PreAuthorize("@ss.hasPermi('system:projectInfo:remove')")
    @Log(title = "项目申报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds) {
        return toAjax(sysProjectInfoService.deleteSysProjectInfoByProjectIds(projectIds));
    }
}
